<template>
  <div>
    <h1>客户管理</h1>
    <br>
    <el-input type="text" v-model="SQEName" placeholder="客户名称" style="width: 10%"></el-input>
    <el-input type="text" v-model="businessScope" placeholder="经营范围" style="width: 10%"></el-input>
    <el-button type="primary" @click="search()">搜索</el-button>
    <br><br>
    <el-row>
      <el-table :data="SQEList" stripe>
        <el-table-column prop="clientName" label="客户名称"></el-table-column>
        <el-table-column prop="principal" label="负责人"></el-table-column>
        <el-table-column prop="scope" label="经营范围"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" @click="see(scope.row)">查看</el-button>

            <el-dialog v-model="pick" title="查看详情" width="30%">
              <el-form :model="form" ref="form">
                <el-form-item label="客户名称">
                  <el-input v-model="form.clientName" disabled></el-input>
                </el-form-item>
                <el-form-item label="负责人">
                  <el-input v-model="form.principal" disabled></el-input>
                </el-form-item>
                <el-form-item label="负责人电话">
                  <el-input v-model="form.principalPhone" disabled></el-input>
                </el-form-item>
                <el-form-item label="办公地址">
                  <el-input v-model="form.location" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="form.mailbox" disabled></el-input>
                </el-form-item>
                <el-form-item label="经营范围">
                  <el-input v-model="form.scope" disabled></el-input>
                </el-form-item>
              </el-form>
              <el-button type="primary" @click="pick=false">确定</el-button>

            </el-dialog>


            <el-button type="primary" @click="alter(scope.row)">修改信息</el-button>

            <el-dialog v-model="update" title="修改" width="30%">
              <el-form :model="form" ref="form">
                <el-form-item label="客户名称">
                  <el-input v-model="form.clientName" disabled></el-input>
                </el-form-item>
                <el-form-item label="负责人">
                  <el-input v-model="form.principal" ></el-input>
                </el-form-item>
                <el-form-item label="负责人电话">
                  <el-input v-model="form.principalPhone" ></el-input>
                </el-form-item>
                <el-form-item label="办公地址">
                  <el-input v-model="form.location" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input v-model="form.mailbox" ></el-input>
                </el-form-item>
                <el-form-item label="经营范围">
                  <el-input v-model="form.scope" disabled></el-input>
                </el-form-item>
              </el-form>
              <el-button type="primary" @click="updateSupplier()">确定</el-button>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>

    </el-row>
    <br>
    <el-pagination v-model:current-page="curPage" @current-change="handleCurrentChange" v-model:page-size="pageSize"
                   background :total="total">

    </el-pagination>

    <el-button type="primary" @click="addSQE()">添加客户</el-button>
    <el-dialog v-model="add" title="添加" width="30%">
      <el-form :model="form" ref="form">
        <el-form-item label="客户名称">
          <el-input v-model="form.clientName" ></el-input>
        </el-form-item>
        <el-form-item label="负责人">
          <el-input v-model="form.principal" ></el-input>
        </el-form-item>
        <el-form-item label="负责人电话">
          <el-input v-model="form.principalPhone" ></el-input>
        </el-form-item>
        <el-form-item label="办公地址">
          <el-input v-model="form.location" ></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.mailbox" ></el-input>
        </el-form-item>
        <el-form-item label="经营范围">
          <el-input v-model="form.scope" ></el-input>
        </el-form-item>
      </el-form>
      <el-button type="primary" @click="addSupplier()">确定</el-button>

    </el-dialog>




    <br><br>
    <div style="width: 300px">
    <h3>导入供应商excel
      <el-upload class="upload"
                 ref="upload"
                 action="string"
                 :file-list="fileList"
      :auto-upload="false"
      :http-request="uploadFile"
      :on-change="handleChange"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      multiple="multiple">
      <el-button slot="trigger"
                 size="small"
                 type="primary"
                 @click="delFile()">选取文件</el-button>
      </el-upload>
      <el-button type="primary"
                 @click="onSubmit()">保存</el-button>
    </h3>
    </div>

  </div>
</template>

<script>
import axios from "axios";
import {ElMessage} from "element-plus";

export default {
  name: "SQE",
  created() {
    this.search()
  },
  data() {
    return {
      SQEList: [],
      curPage: 1,
      pageSize: 3,
      total: 0,
      SQEName: '',
      businessScope: '',
      pick: false,
      update:false,
      add:false,
      form: {
        clientName: '',
        principal:'',
        principalPhone:'',
        location:'',
        scope:'',
        mailbox:'',
        fileList: [],
        // 不支持多选
        multiple: false,
        formData: "",

      }
    }
  }, methods: {
    //点击上传文件触发的额外事件,清空fileList
    delFile () {
      this.fileList = [];
    },
    handleChange (file, fileList) {
      this.fileList = fileList;
      // console.log(this.fileList, "sb");
    },
    //自定义上传文件
    uploadFile (file) {
      this.formData.append("file", file.file);
      // console.log(file.file, "sb2");
    },
    //删除文件
    handleRemove (file, fileList) {
      console.log(file, fileList);
    },
    // 点击文件
    handlePreview (file) {
      console.log(file);
    },onSubmit(){
      let formData = new FormData();
      formData.append("file", this.fileList[0].raw);//拿到存在fileList的文件存放到formData中
      //下面数据是我自己设置的数据,可自行添加数据到formData(使用键值对方式存储)
      formData.append("title", this.title);
      axios.post("/api/client/importExcel", formData, {
        "Content-Type": "multipart/form-data;charset=utf-8"
      })
          .then(res => {
            if (res.data === "SUCCESS") {
              this.$notify({
                title: '成功',
                message: '提交成功',
                type: 'success',
                duration: 1000
              });
            }
          })
    },






    handleCurrentChange(page) {

      this.curPage = page;

      this.search()

    },
    search() {
      let param = new URLSearchParams();
      param.append("pageNo", this.curPage)
      param.append("pageSize", this.pageSize)
      param.append("name", this.SQEName)
      param.append("scope", this.businessScope)
      console.log(this.businessScope)
      axios.post("/api/client/selectClient", param).then(res => {

        console.log(res.data.records)
        this.SQEList = res.data.records;
        this.total = res.data.total
        this.SQEName = '';
        this.businessScope = ''
      })


    },
    see(scope) {
      this.pick = true;
      this.form = scope


    },
    alter(scope) {
      this.update=true
      this.form=scope

    },updateSupplier(){
      let param=this.form
      console.log(param)
      axios.post("/api/client/updateClient",param).then(res=>{
        this.update=false;
        ElMessage({
          message: '修改成功',
          type: 'success'
        })
      })





    },
    addSQE() {
      this.add=true

    },
    addSupplier(){
      let param=this.form
      console.log(param)
      axios.post("/api/client/addClient",param).then(res=>{
        this.add=false;
        ElMessage({
          message: '添加成功',
          type: 'success'
        })
      })





    }




  }
}
</script>

<style scoped>

</style>